package com.item 
{
	import com.data.GameData;
	import com.greensock.easing.Circ;
	import com.greensock.TweenLite;
	import com.sound.SoundCtrl;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.setTimeout;
	/**
	 * ...
	 * @author wen
	 */
	public class Bird extends ItemBase 
	{
		
		public const R:Number = 90;
		
		public const Nums:int = 12;
		
		private var _stars:Array = null;
		
		public function Bird(mc:MovieClip) 
		{
			super(mc);
		}
		
		override public function init():void 
		{
			_stars = new Array();
			for (var i:int = 0; i < Nums; i++ )
			{
				var o:SuperStar = new SuperStar();
				o.id = i;
				_stars.push(o);
				view.addChild(o);
				o.scaleX = o.scaleY = 1.2;
			}
			
			clear();
			
			show();
		}
		
		
		public function show():void
		{
			view.a.visible = true;
			view.a.gotoAndPlay(1);
			
			
			view.addEventListener(Event.ENTER_FRAME, onFlyHander);
		}
		
		private function onFlyHander(e:Event):void 
		{
			var s:Number = 5;
			view.x += (view.scaleX == 1)?s: -s;
			if (view.x < 5)
			{
				view.scaleX = 1;
			}
			else if (view.x > GameData.gameWidth-5)
			{
				view.scaleX = -1;
			}
		}
		
		public function stepOn():void
		{
			SoundCtrl.instance.soundPlay("bird");
			
			view.a.visible = false;
			view.a.gotoAndStop(1);
			
			view.b.visible = true;
			view.b.gotoAndPlay(1);
			
			
			view.removeEventListener(Event.ENTER_FRAME, onFlyHander);
			
			showStars();
			
			setTimeout(hide, 500);
		}
		
		/**
		 * 隐藏
		 */
		public function hide():void
		{
			for (var i:int = 0; i < Nums; i++ )
			{
				TweenLite.to(_stars[i], 0.5, { y:_stars[i].y+40, alpha:0} );
			}
			
			if (view.scaleX == 1)
			{
				TweenLite.to(view, 1, { x:780, ease:Circ.easeIn, onComplete:clearView } );
			}else
			{
				TweenLite.to(view, 1, { x:-20, ease:Circ.easeIn, onComplete:clearView } );
			}
		}
		
		/**
		 * 显示星星效果
		 * @param	scale
		 */
		public function showStars():void
		{
			for (var i:int = 0; i < Nums; i++ )
			{
				_stars[i].visible = true;
				_stars[i].alpha = 0.5;
				_stars[i].rotation = 0;
				_stars[i].angle = i*2 * Math.PI / Nums;
				
				//var tmpX:Number = _stars[i].x + R * Math.cos(i*2 * Math.PI / Nums);
				//var tmpY:Number = _stars[i].y + R * Math.sin(i*2 * Math.PI / Nums);
				
				TweenLite.to(_stars[i], 0.5, { rotation:360 , angle:((i+4)*2 * Math.PI / Nums) , alpha:1  } );
			}
		}
		
		public function clearView():void 
		{
			while (view.numChildren > 1)
			{
				view.removeChildAt(0);
			}
			view.parent.removeChild(view);
		}
		
		override public function clear():void 
		{
			view.a.gotoAndStop(1);
			view.b.gotoAndStop(1);
			view.a.visible = false;
			view.b.visible = false;
			
			for (var i:int = 0; i < _stars.length; i++ )
			{
				(_stars[i] as MovieClip).visible = false;
				(_stars[i] as MovieClip).x = view.x;
				(_stars[i] as MovieClip).y = view.y;
			}
		}
		
	}

}